package pl.edu.agh.administracja.networkconfig.view.model;

import java.util.List;
import javax.swing.table.AbstractTableModel;
import pl.edu.agh.administracja.networkconfig.model.WirelessInterfaceConfig;
import pl.edu.agh.administracja.networkconfig.model.WirelessNetwork;
import pl.edu.agh.administracja.networkconfig.model.util.WirelessNetworkResolver;

/**
 * Model dla tabeli z sieciami bezprzewodowymi.
 * @author piotrek
 */
public class WirelessNetworkTableModel extends AbstractTableModel {
    public static final int WIRELESS_NETWORK_TABLE_MODEL_COLUMN_COUNT = 3;
    
    private WirelessInterfaceConfig wirelessInterfaceConfig;
    private List<WirelessNetwork> availableNetworks = null;
    private String[] columnNames = {"Nazwa", "Typ zabezpieczeń", "Siła"};

    public WirelessNetworkTableModel(WirelessInterfaceConfig wirelessInterfaceConfig) {
        this.wirelessInterfaceConfig = wirelessInterfaceConfig;
    }
    
    public void init(){
        List<WirelessNetwork> list = WirelessNetworkResolver.getAvailableNetworks(wirelessInterfaceConfig);
        
        synchronized(WirelessNetworkTableModel.this){
            availableNetworks = list;
            WirelessNetworkTableModel.this.notifyAll();
        }
    }
    
    @Override
    public int getRowCount() {
        return availableNetworks.size();
    }

    @Override
    public int getColumnCount() {
        return WIRELESS_NETWORK_TABLE_MODEL_COLUMN_COUNT;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        WirelessNetwork wirelessNetwork = availableNetworks.get(rowIndex);
        
        switch(columnIndex){
            case 0:{
                return wirelessNetwork.getESSID();
            }
                
            case 1:{
                return wirelessNetwork.getAuthenticationType();
            }
                
            case 2:{
                return wirelessNetwork.getQuality();
            }
        }
        
        return null;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
    
    public WirelessNetwork getWirelessNetworkAt(int row){
        return availableNetworks.get(row);
    }
    
   public synchronized boolean isInitialized(){
       return availableNetworks != null;
   }
    
}
